﻿using blackhouse.Aparts.Web;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Web;
using blackhouse.BlackApart.Web.Navigation;
using blackhouse.StareKino.Web.Modules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.StareKino.Web.Events {
    public partial class PostFreeApartments : System.Web.UI.UserControl {

        #region Fields

        private CinemaPostModel postModel;
        private FreeApartments findApartments;
        private SearchState state;

        #endregion

        #region Properties

        public string CssClass {
            get {
                return this.pnlFreeApartments.CssClass;
            }
            set {
                this.pnlFreeApartments.CssClass = value;
            }
        }

        #endregion

        #region Methods

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (this.SetScenario())
                this.PopulateView();
            else
                this.pnlFreeApartments.Visible = false;
        }

        private bool SetScenario() {
            this.postModel = new CinemaPostModel();
            if (this.postModel.Post == null || this.postModel.Settings == null) return false;
            DateTime eventDate = this.postModel.Post.GetEventDate();
            this.state = SearchState.GetState(
                eventDate.Date.AddDays(-1).AddHours(14), // sugerujemy przyjazd dzień wcześniej
                eventDate.Date.AddDays(1).AddHours(12),  // sugerujemy wyjazd dzień po
                1);
            if (state.ArrivalDate.Date < DateTime.Now.Date) return false;

            this.findApartments = new FreeApartments(state.ArrivalDate, state.DepartureDate, state.PeopleCount);
            if (!this.findApartments.Any()) return false; // brak wolnych pokoi
            return true;
        }

        private void PopulateView() {
            this.ltlDateRange.Text = this.state.ArrivalDate.FormatDateRange(this.state.DepartureDate);
            this.rptApartments.DataSource = this.GetApartmentsDataSource();
            this.rptApartments.DataBind();
            this.lnkToMoreApartments.NavigateUrl = NavigationProvider.Instance().GetUrl(this.state);
            if (this.lnkToMoreApartments.Text.Contains("{0}"))
                this.lnkToMoreApartments.Text = String.Format(this.lnkToMoreApartments.Text, this.ltlDateRange.Text);
            if (this.lnkToMoreApartments.ToolTip.Contains("{0}"))
                this.lnkToMoreApartments.ToolTip = String.Format(this.lnkToMoreApartments.ToolTip, this.ltlDateRange.Text);
        }

        private IEnumerable<ApartmentGroupWithVariant> GetApartmentsDataSource() {
            List<ApartmentGroupWithVariant> gr = new List<ApartmentGroupWithVariant>();
            foreach (Variant v in this.findApartments.OrderByDescending(v => v.GetGroups().First().Popularity).Take(3).OrderBy(v => v.Price.GetPrice()).ThenBy(v => v.GetGroups().First().Name)) {
                gr.Add(new ApartmentGroupWithVariant(v.GetGroups().OrderByDescending(g => g.NumberOfPeople).First(), v));
            }
            return gr;
        }

        #endregion

        protected void rptApartments_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:
                    IApartmentGroup group = e.Item.DataItem as IApartmentGroup;
                    ApartmentGroupWithVariant groupWithVariant = group as ApartmentGroupWithVariant;
                    if (group != null) {
                        ApartmentGroupLine line = e.Item.FindControl("ctrlApartmentGroupLine") as ApartmentGroupLine;

                        if (line != null) {
                            line.QueryState = this.state;
                            line.PopulateControl(group, groupWithVariant.Variant);
                        }
                        else
                            e.Item.Visible = false;
                    }
                    else
                        e.Item.Visible = false;
                    break;
            }
        }

    }
}